package cn.wk.config;

import cn.wk.certification.MyAuthenticationSuccessHandler;
import cn.wk.domain.Permission;
import cn.wk.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.List;

@Configuration
@EnableWebSecurity  //开启Security
@EnableGlobalMethodSecurity(prePostEnabled = true)  //开启PreAuthorize PostAuthorize授权注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PermissionMapper permissionMapper;

/*    //基于内存中的user
    @Bean
    public UserDetailsService userDetailsServiceBean() throws Exception {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
        return inMemoryUserDetailsManager;
    }*/


    //字符编码
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {

       /* //查出所有权限
        List<Permission> permissions = permissionMapper.selectAll();

        //添加某个资源需要用户拥有什么样的权限才能访问
        permissions.forEach(permission -> {
            try {
                http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getExpression());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });*/

        http.authorizeRequests()
                .antMatchers("/login.html").permitAll()//该资源路径放行
                .anyRequest().authenticated()   //其他路径都要认证后才能访问
                .and().formLogin()      //允许表单登录
                .successHandler(new MyAuthenticationSuccessHandler())
                //.successForwardUrl("/testcontroller/list") //登录成功跳转页
                .and().logout().permitAll()     //登出放行
                .and().csrf().disable();        //关闭跨域伪造检查
    }
}
